package com.gt.deceive.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gt.deceive.entity.ReceiveAddress;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * 地点生成
 * 百度地图接口地址：http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition
 */
public class CreateAddUtil {
    private static String ak = "9cf20f1365e7ee69b55f7b33ad1e9e84";
    public static void main(String[] args) {

        List<ReceiveAddress> l = addToAddList("北京市北京市昌平区天通苑北一区","5000", 100);
        System.out.println(l.size());
        l.forEach(item ->{
            System.out.println(item.getProvinceName()+item.getCityName()+item.getDistrictName()+item.getAddress());
        });
    }

    /**
     * 根据水站地址生成附件的小区地址
     * @param address  水站地址     eg：北京市北京市海淀区西三环北路72号世纪经贸大厦
     * @param radius   半径（米)    eg：50
     * @param num      生成个数     eg：20
     * @return
     */
    public static List<ReceiveAddress> addToAddList(String address, String radius, int num){
        JSONObject jsonObject = addToPoint(address);
        List<ReceiveAddress> list = new ArrayList<ReceiveAddress>();
        String lat = JSONObject.parseObject(JSONObject.parseObject(jsonObject.getString("result")).getString("location")).getString("lat");
        String lng = JSONObject.parseObject(JSONObject.parseObject(jsonObject.getString("result")).getString("location")).getString("lng");
        JSONObject json = getAdd(lat+","+ lng, radius);

        JSONArray jsonArray = JSONArray.parseArray(json.getString("results"));
        if(jsonArray != null && !jsonArray.isEmpty()){
            int cyclic_time = num>=jsonArray.size()?(num/jsonArray.size()):0;
            int surplus = num%jsonArray.size();
            jsonArray.forEach(item ->{
                JSONObject j = JSONObject.parseObject(item.toString());
                for(int i=0;i < cyclic_time; i++){
                    list.add(jsonToReceiveAddress(j));
                }
            });
            if(0 != surplus){
                JSONObject j = JSONObject.parseObject(jsonArray.get(0).toString());
                for(int i=0;i<surplus;i++){
                    list.add(jsonToReceiveAddress(j));
                }
            }

        }

        return list;
    }

    public static ReceiveAddress jsonToReceiveAddress(JSONObject json){
        ReceiveAddress r = new ReceiveAddress();
        r.setProvinceName(json.getString("province"));
        r.setCityName(json.getString("city"));
        r.setDistrictName(json.getString("area"));
        r.setAddress(json.getString("address")+json.getString("name")+getNumber(20)+"号楼"+getNumber(10)+"单元"+getNumber(20)+"0"+getNumber(5)+"室");
        return r;
    }

    public static int getNumber(int number){
        Random r = new Random();
        return r.nextInt(number)+1;
    }

    //生成某个地点周围的地址
    public static JSONObject getAdd(String location, String radius){
        String url = "http://api.map.baidu.com/place/v2/search?query=住宅区$小区&tag=写字楼,住宅区,宿舍&location="+location+"&radius="+radius+"&output=json&ak="+ak;
        JSONObject json = null;
        HttpHelper h = new HttpHelper();
        HashMap p = new HashMap();
        try {
            String res = h.postData2(url, p, "utf-8", 10, 2);
            json = JSONObject.parseObject(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    //地址转经纬度
    public static JSONObject addToPoint(String add){
        String url = "http://api.map.baidu.com/geocoder/v2/?address="+add+"&output=json&ak="+ ak;
        HttpHelper h = new HttpHelper();
        JSONObject json = null;
        try {
            String res = h.postData2(url, new HashMap<>(), "utf-8", 10, 2);
            json = JSONObject.parseObject(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
